package com.snts.synchronization.core;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class ErrorObject {

	public static final int AUTHORIZATION_FAILURE = 1;
	public static final int INTERNAL_ERROR = 2;
	public static final int SESSION_TIMEOUT = 3;

	private int errorCode = 2;
	private String errorMessage = "";
	private Throwable throwable;
	private String additionalInformation = "";

	public static final String errorCodeToString(int ec) {
		switch (ec) {
			case AUTHORIZATION_FAILURE:
				return "AUTHORIZATION_FAILURE";
			case INTERNAL_ERROR:
				return "INTERNAL_ERRROR";
			case SESSION_TIMEOUT:
				return "SESSION_TIMEOUT";
			default:
				return "INTERNAL_ERROR";
		}
	}

	/**
	 * 
	 */
	public ErrorObject() {
		super();
		// TODO Auto-generated constructor stub
	}

	public ErrorObject(int errorCode, String errorMessage, Throwable throwable) {
		super();
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
		this.throwable = throwable;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public void setAdditionalInformation(String additionalInformation) {
		this.additionalInformation = additionalInformation;
	}

	public Throwable getThrowable() {
		return throwable;
	}

	public void setThrowable(Throwable throwable) {
		this.throwable = throwable;
	}

	public String getAdditionalInformation() {
		if (additionalInformation == null && throwable != null) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PrintWriter printWriter = new PrintWriter(baos);
			throwable.printStackTrace(printWriter);
			additionalInformation = new String(baos.toByteArray());
		}
		return additionalInformation;
	}

	@Override
	public String toString() {
		return "ErrorObject [errorCode=" + errorCode + ", errorMessage=" + errorMessage + ", additionalInformation=" + getAdditionalInformation() + "]";
	}
	
	

}
